package com.mine.frame.mylandemo.mvp.presenter

import android.util.Log
import com.mine.frame.common.base.BasePresenter
import com.mine.frame.mylandemo.entity.forick.ControlBean
import com.mine.frame.mylandemo.mvp.iview.IBusDoor
import com.third.device.mg.serialport.MGSerialPort
import com.third.device.mg.serialport.utils.bitToByte
import com.third.device.mg.serialport.utils.toBitString
import com.third.device.mg.serialport.utils.toHexString
import com.third.device.mg.serialport.utils.toIntU

/**
 * @Date 2022/3/2
 * Description:
 */
class BusDoorPresenter(baseView: IBusDoor) : BasePresenter<IBusDoor>(baseView) {

    private val devId = 0x50.toByte()

    /**
     * 创建控制数据
     */
    fun createControlData() {
        val list = mutableListOf<ControlBean>()
        list.add(ControlBean(true, "欧派克电动巴士门控制"))



        val bcStudy = ControlBean(false, "广播初始化学习")
        bcStudy.command = MGSerialPort.instance.getOuPaiKe().busDoorDevice().bcInitDevice()
        list.add(bcStudy)

        val study = ControlBean(false, "指定设备初始化学习")
        study.command = MGSerialPort.instance.getOuPaiKe().busDoorDevice().initDevice(devId)
        list.add(study)



        val bcAddress = ControlBean(false, "广播获取设备地址")
        bcAddress.command = MGSerialPort.instance.getOuPaiKe().busDoorDevice().bcAddress()
        bcAddress.what = 0
        list.add(bcAddress)


        val bcType = ControlBean(false, "广播获取设备类型")
        bcType.command = MGSerialPort.instance.getOuPaiKe().busDoorDevice().bcDeviceType()
        bcType.what = 1
        list.add(bcType)

        val idType = ControlBean(false, "ID获取设备类型")
        idType.command = MGSerialPort.instance.getOuPaiKe().busDoorDevice().getDeviceType(devId)
        idType.what = 1
        list.add(idType)

        val uID51 = ControlBean(false, "设置更改id为0x51")
        uID51.command = MGSerialPort.instance.getOuPaiKe().busDoorDevice().setDeviceId(devId, 0x51)
        list.add(uID51)

        val uID50 = ControlBean(false, "广播更改id为0x50")
        uID50.command = MGSerialPort.instance.getOuPaiKe().busDoorDevice().cbSetDeviceId(devId)
        list.add(uID50)


        val state = ControlBean(false, "查询设备状态")
        state.command = MGSerialPort.instance.getOuPaiKe().busDoorDevice().getDeviceState(devId)
        state.what = 2
        list.add(state)



        val speed = ControlBean(false, "查询设备速度")
        speed.command = MGSerialPort.instance.getOuPaiKe().busDoorDevice().getDeviceSpeed(devId)
        speed.what = 4
        list.add(speed)

        val speedList = arrayListOf(30 ,55 ,80)
        speedList.forEach {
            val initSpeed = ControlBean(false, "设置速度$it")
            initSpeed.command = MGSerialPort.instance.getOuPaiKe().busDoorDevice().setSpeed(devId ,it)
            list.add(initSpeed)
        }


        val openTime = ControlBean(false, "开门时间")
        openTime.command = MGSerialPort.instance.getOuPaiKe().busDoorDevice().getDeviceOpenTime(devId)
        openTime.what = 5
        list.add(openTime)

        val autoClose = ControlBean(false, "是否自动关门")
        autoClose.command = MGSerialPort.instance.getOuPaiKe().busDoorDevice().getDeviceIsAutoClose(devId)
        autoClose.what = 6
        list.add(autoClose)


        val reversal = ControlBean(false, "是否开门换向")
        reversal.command = MGSerialPort.instance.getOuPaiKe().busDoorDevice().getDeviceIsReversal(devId)
        reversal.what = 7
        list.add(reversal)

        val autoLock = ControlBean(false, "是否自动上锁")
        autoLock.command = MGSerialPort.instance.getOuPaiKe().busDoorDevice().getDeviceIsAutoLock(devId)
        autoLock.what = 8
        list.add(autoLock)


        val sense = ControlBean(false, "是否门头感应")
        sense.command = MGSerialPort.instance.getOuPaiKe().busDoorDevice().getDeviceIsSense(devId)
        sense.what = 9
        list.add(sense)


        val force = ControlBean(false, "是否保持力")
        force.command = MGSerialPort.instance.getOuPaiKe().busDoorDevice().getDeviceIsForce(devId)
        force.what = 10
        list.add(force)


        val syncCenter = ControlBean(false, "是否中门同步")
        syncCenter.command = MGSerialPort.instance.getOuPaiKe().busDoorDevice().getDeviceIsSyncCenter(devId)
        syncCenter.what = 11
        list.add(syncCenter)


        val syncSide = ControlBean(false, "是否边门同步")
        syncSide.command = MGSerialPort.instance.getOuPaiKe().busDoorDevice().getDeviceIsSyncSide(devId)
        syncSide.what = 12
        list.add(syncSide)


        val reversalFalse = ControlBean(false, "关闭开门换向")
        reversalFalse.command = MGSerialPort.instance.getOuPaiKe().busDoorDevice().setDeviceReversal(devId ,false)
        list.add(reversalFalse)

        val reversalTrue= ControlBean(false, "开启开门换向")
        reversalTrue.command = MGSerialPort.instance.getOuPaiKe().busDoorDevice().setDeviceReversal(devId ,true)
        list.add(reversalTrue)




        val setAutoTrue = ControlBean(false, "设置自动关门")
        setAutoTrue.command = MGSerialPort.instance.getOuPaiKe().busDoorDevice().setDeviceAutoClose(devId ,true)
        list.add(setAutoTrue)

        val setAutoFalse = ControlBean(false, "关闭自动关门")
        setAutoFalse.command = MGSerialPort.instance.getOuPaiKe().busDoorDevice().setDeviceAutoClose(devId ,false)
        list.add(setAutoFalse)


        val setSyncCenterFalse = ControlBean(false, "取消中门同步")
        setSyncCenterFalse.command = MGSerialPort.instance.getOuPaiKe().busDoorDevice().setSyncCenter(devId ,false)
        list.add(setSyncCenterFalse)

        val setSyncCenterTrue = ControlBean(false, "设置中门同步")
        setSyncCenterTrue.command = MGSerialPort.instance.getOuPaiKe().busDoorDevice().setSyncCenter(devId ,true)
        list.add(setSyncCenterTrue)



        val setSyncSideFalse = ControlBean(false, "取消边门同步")
        setSyncSideFalse.command = MGSerialPort.instance.getOuPaiKe().busDoorDevice().setSyncSide(devId ,false)
        list.add(setSyncSideFalse)

        val setSyncSideTrue = ControlBean(false, "设置边门同步")
        setSyncSideTrue.command = MGSerialPort.instance.getOuPaiKe().busDoorDevice().setSyncSide(devId ,true)
        list.add(setSyncSideTrue)


        val openCenter = ControlBean(false, "打开中间")
        openCenter.command = MGSerialPort.instance.getOuPaiKe().busDoorDevice().controlDoor(devId ,0x00 ,0x01 ,0x01 ,0x00)
        list.add(openCenter)

        val closeCenter = ControlBean(false, "关闭中间")
        closeCenter.command = MGSerialPort.instance.getOuPaiKe().busDoorDevice().controlDoor(devId ,0x00 ,0x02 ,0x02 ,0x00)
        list.add(closeCenter)


        val openSide = ControlBean(false, "打开两边")
        openSide.command = MGSerialPort.instance.getOuPaiKe().busDoorDevice().controlDoor(devId ,0x01 ,0x00 ,0x00 ,0x01)
        list.add(openSide)

        val closeSide = ControlBean(false, "关闭两边")
        closeSide.command = MGSerialPort.instance.getOuPaiKe().busDoorDevice().controlDoor(devId ,0x02 ,0x00 ,0x00 ,0x02)
        list.add(closeSide)


        val open1= ControlBean(false, "打开1")
        open1.command = MGSerialPort.instance.getOuPaiKe().busDoorDevice().controlDoor(devId ,0x01 ,0x00 ,0x00 ,0x00)
        list.add(open1)

        val open2= ControlBean(false, "打开2")
        open2.command = MGSerialPort.instance.getOuPaiKe().busDoorDevice().controlDoor(devId ,0x00 ,0x01 ,0x00 ,0x00)
        list.add(open2)

        val open3= ControlBean(false, "打开3")
        open3.command = MGSerialPort.instance.getOuPaiKe().busDoorDevice().controlDoor(devId ,0x00 ,0x00 ,0x01 ,0x00)
        list.add(open3)

        val open4= ControlBean(false, "打开4")
        open4.command = MGSerialPort.instance.getOuPaiKe().busDoorDevice().controlDoor(devId ,0x00 ,0x00 ,0x00 ,0x01)
        list.add(open4)

        val closeAll= ControlBean(false, "全部关闭")
        closeAll.command = MGSerialPort.instance.getOuPaiKe().busDoorDevice().controlDoor(devId ,0x02 ,0x02 ,0x02 ,0x02)
        list.add(closeAll)


        val open13= ControlBean(false, "打开13")
        open13.command = MGSerialPort.instance.getOuPaiKe().busDoorDevice().controlDoor(devId ,0x01 ,0x00 ,0x01 ,0x00)
        list.add(open13)

        val close13 = ControlBean(false, "关闭13")
        close13.command = MGSerialPort.instance.getOuPaiKe().busDoorDevice().controlDoor(devId ,0x02 ,0x00 ,0x02 ,0x00)
        list.add(close13)


        val open24= ControlBean(false, "打开24")
        open24.command = MGSerialPort.instance.getOuPaiKe().busDoorDevice().controlDoor(devId ,0x00 ,0x01 ,0x00 ,0x01)
        list.add(open24)

        val close24 = ControlBean(false, "关闭24")
        close24.command = MGSerialPort.instance.getOuPaiKe().busDoorDevice().controlDoor(devId ,0x00 ,0x02 ,0x00 ,0x02)
        list.add(close24)



        mIView.initControlList(list)
    }


    fun parseCommand(item: ControlBean, command: ByteArray) {
        if (command.size < 12) return
        var msg = ""
        when (item.what) {
            0 -> {
                val id = command[2]
                msg = "设备地址: 0x${id.toHexString()}  ==>>  ${id.toIntU()} "
            }

            1 -> {
                val type = command[6]
                when (type.toIntU()) {
                    0x01 -> {
                        msg = "设备类型: 0x${type.toHexString()}  ==>>  ${type.toIntU()}  单磁悬浮门"
                    }
                    0x02 -> {
                        msg = "设备类型: 0x${type.toHexString()}  ==>>  ${type.toIntU()}  磁悬浮对开门"
                    }
                    0x03 -> {
                        msg = "设备类型: 0x${type.toHexString()}  ==>>  ${type.toIntU()}   单平躺门"
                    }
                    0x04 -> {
                        msg = "设备类型: 0x${type.toHexString()}  ==>>  ${type.toIntU()}   双平躺门"
                    }
                    0x05 -> {
                        msg = "设备类型: 0x${type.toHexString()}  ==>>  ${type.toIntU()}   单自由折叠门"
                    }
                    0x06 -> {
                        msg = "设备类型: 0x${type.toHexString()}  ==>>  ${type.toIntU()}   双自由折叠门"
                    }
                }
            }

            2 -> {
                val result = StringBuffer()
                val d1Bit = command[6].toBitString()
                result.append("门1:")
                result.append(doorState(d1Bit))
                result.appendLine()

                val d2Bit = command[7].toBitString()
                result.append("门2:")
                result.append(doorState(d2Bit))
                result.appendLine()

                val d3Bit = command[8].toBitString()
                result.append("门3:")
                result.append(doorState(d3Bit))
                result.appendLine()

                val d4Bit = command[9].toBitString()
                result.append("门4:")
                result.append(doorState(d4Bit))
                result.appendLine()

                msg = result.toString()
            }

            4 -> {
                msg ="设备速度: ${command[6].toIntU()}   0~100"
            }

            5 -> {
                msg ="设备开门时间 : ${command[6].toIntU()}s  0~30"
            }

            6 -> {
                msg ="是否自动关门 : ${command[6].toIntU() == 1 }"
            }

            7 -> {
                msg ="是否开门换向 : ${command[6].toIntU() == 1 }"
            }

            8 -> {
                msg ="是否自动上锁 : ${command[6].toIntU() == 1 }"
            }

            9 -> {
                msg ="门头感应 : ${command[6].toIntU() == 1 }"
            }

            10 -> {
                msg ="保持力 : ${command[6].toIntU() == 1 }"
            }

            11 -> {
                msg ="中门同步 : ${command[6].toIntU() == 1 }"
            }

            12 -> {
                msg ="边门同步 : ${command[6].toIntU() == 1 }"
            }

        }
        Log.e("巴士门 ==>", msg)
        mIView.setResultMsg(msg)
    }


    /**
     * 获取设备状态
     * 一共4个Byte，每个Byte对应一扇门共4扇，最低位Byte对应门1
     * 每个Byte的bit0-bit5：
     * 0x00：门初始化中
     * 0x01：门已关闭
     * 0x02：门开启中
     * 0x03：门已打开
     * 0x04：门关闭中
     * 0x05：门暂停
     * Bit6：上锁状态
     * 0：未上锁
     * 1：已上锁
     * Bit7：报警状态
     * 0：无报警
     * 1：有报警
     */
    fun doorState(state: String): String {
        val str = when (state.substring(3).bitToByte().toIntU()) {
            0x00 -> {
                "门初始化中"
            }
            0x01 -> {
                "门已关闭"
            }
            0x02 -> {
                "门开启中"
            }
            0x03 -> {
                "门已打开"
            }
            0x04 -> {
                "门关闭中"
            }
            0x05 -> {
                "门暂停"
            }
            else -> {
                "未知"
            }
        }
        val result = StringBuffer(str)
        if ('0' == state[2]) {
            result.append(" 未上锁")
        } else {
            result.append(" 已上锁")
        }

        if ('0' == state[1]) {
            result.append(" 无报警")
        } else {
            result.append(" 有报警")
        }
        return result.toString()
    }
}